/**
 * Author       : MadFrog
 * Date         : 2018/3/17 10:45
 * Description  :
 */

Ext.namespace("admin.questionnaire");

admin.questionnaire.manage = function(panelId, title, iconClz){
    // 用户操作标志 默认开启 在clazzStore加载的时候关闭
    var userOprationFlag = true;
    var questionnaireStore = new Ext.data.JsonStore({
        url : "admin/questionnaire/findByCon",
        fields : ["id", "title", "name", "closeDate", "description", "createDate"],
        totalProperty : "total",
        root : "data",
        autoLoad : true
    });
    var questionnaireSm = new Ext.grid.RowSelectionModel({
        singleSelect : true,
        listeners : {
            rowselect : function () {
                clazzStore.reload();
            }
        }
    });
    var questionnaireCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header : "问卷名称",
        dataIndex : "name",
        width : 200
    }, {
        header : "说明",
        dataIndex : "description",
        width : 200
    }, {
        header : "状态",
        dataIndex : "status",
        width : 100
    }, {
        header : "截止时间",
        dataIndex : "closeDate",
        renderer : renderDate
    }, {
        header : "创建时间",
        dataIndex : "createDate",
        renderer : renderDate
    }]);
    var questionnaireGrid = new Ext.grid.GridPanel({
        title : "问卷列表",
        store : questionnaireStore,
        cm : questionnaireCm,
        sm : questionnaireSm,
        frame : true,
        flex : 5,
        tbar : genOptBarByFn(createQuestionnaire),
        bbar : genPageBar(questionnaireStore),
        listeners : {
            rowdblclick : function (grid, rowIndex) {
                //rowIndex 从0开始
                // 弹出窗口
                showSubjectByQuestionnaire();
            }
        }
    });
    var clazzStore = new Ext.data.JsonStore({
        url : "admin/questionnaire/listClazzDto",
        fields : ["id", "name", "studentNumber", "selected"],
        root : "data",
        listeners : {
            beforeload : function(s){
                s.removeAll();
                Ext.apply(s.baseParams, {
                    examId : questionnaireSm.getSelected().get("id")
                });
            },
            load : function (store, records) {
                userOprationFlag = false;
                var range = [];
                for(var i=0; i<records.length; i++){
                    var r = records[i];
                    if(r.get("selected")==true){
                        range.push(i);
                    }
                }
                clazzSm.selectRows(range);
                userOprationFlag = true;
            }
        }
    });
    var clazzSm = new Ext.grid.CheckboxSelectionModel({
        checkOnly : true,
        listeners : {
            rowdeselect : function (sm, rowIndex, record) {
                if (userOprationFlag==true) {
                    var questionnaireId = questionnaireSm.getSelected().get("id");
                    var clazzId = record.get("id")
                    ajaxRequest({
                        url: "admin/questionnaire/delClazz/" + questionnaireId + "/" + clazzId
                    });
                }
            },
            rowselect : function (sm, rowIndex, record) {
                if (userOprationFlag==true) {
                    var questionnaireId = questionnaireSm.getSelected().get("id");
                    var clazzId = record.get("id")
                    ajaxRequest({
                        url: "admin/questionnaire/addClazz/" + questionnaireId + "/" + clazzId
                    });
                }
            }
        }
    });
    var clazzCm = new Ext.grid.ColumnModel([clazzSm, new Ext.grid.RowNumberer(), {
        header : "班级名称",
        dataIndex : "name",
        width : 200
    }, {
        header : "学生人数",
        dataIndex : "studentNumber",
        width : 60
    }]);
    var clazzGrid = new Ext.grid.GridPanel({
        title : "班级列表",
        store : clazzStore,
        cm : clazzCm,
        sm : clazzSm,
        frame : true,
        flex : 2
    });
    return new Ext.Panel({
        id : panelId,
        title : title,
        iconCls : iconClz,
        closable : true,
        layout : "hbox",
        layoutConfig : {
            align : "stretch"
        },
        items : [questionnaireGrid, clazzGrid]
    });

    function createQuestionnaire() {
        genAddorUpdateWin("admin/questionnaire/add").show();
    }
    function genAddorUpdateWin(url){
        return new Ext.Window({
            title : "新增或修改问卷调查",
            layout : "fit",
            modal : true,
            iconCls : "save",
            height : 230,
            width : 290,
            bodyStyle : 'padding:5px;',
            items : genForm(url)
        });
        function genForm(url){
            return new Ext.form.FormPanel({
                url : url,
                labelWidth : 80,
                labelAlign : "right",
                baseCls: 'x-plain',
                defaults : {
                    width : 150
                },
                items : [{
                    xtype : "hidden",
                    name : "id",
                    //value设置-1，为空则controller检验失败
                    value : "-1"
                }, {
                    xtype : "textfield",
                    fieldLabel : "名称",
                    allowBlank : false,
                    name : "name"
                }, {
                    fieldLabel : "截止日期",
                    allowBlank : false,
                    name : "closeDate",
                    xtype : "datefield",
                    format : "Y-m-d"
                }, {
                    xtype : "textarea",
                    fieldLabel : "说明",
                    allowBlank : false,
                    name : "description"
                }],
                buttonAlign : "center",
                buttons : [{
                    text : "保存",
                    iconCls : "save",
                    handler : function(b){
                        submitForm(b.findParentByType("form"), refreshEventGrid);
                        function refreshEventGrid(){
                            questionnaireStore.reload();
                        }
                    }
                }, {
                    text : "重置",
                    iconCls : "reset",
                    type : "reset",
                    handler : function(b){
                        b.findParentByType("form").getForm().reset();
                    }
                }]
            });
        }
    }
    function showSubjectByQuestionnaire() {
        var subjectStore = new Ext.data.JsonStore({
            url : "admin/questionnaire/subject/listByQuestionnaire",
            totalProperty : "total",
            root : "data",
            autoLoad : true,
            fields : ["id", "num", "stem", "choice1", "choice2", "choice3", "choice4", "choice5"],
            listeners : {
                beforeload : function(store){
                    var rec = questionnaireSm.getSelected();
                    if(rec==undefined){
                        return false;
                    }
                    store.removeAll();
                    Ext.apply(store.baseParams, {
                        questionnaireId : rec.get("id")
                    });
                }
            }
        });
        var subjectCm = new Ext.grid.ColumnModel([{
            header : "序号",
            dataIndex : "num",
            width : 50
        }, {
            header : "题目",
            dataIndex : "stem",
            width : 200
        }, {
            header : "选项1",
            dataIndex : "choice1"
        }, {
            header : "选项2",
            dataIndex : "choice2"
        }, {
            header : "选项3",
            dataIndex : "choice3"
        }, {
            header : "选项4",
            dataIndex : "choice4"
        }, {
            header : "选项5",
            dataIndex : "choice5"
        }]);
        var subjectSm = new Ext.grid.RowSelectionModel({
            singleSelect : true
        });
        var subjectGrid = new Ext.grid.GridPanel({
            // frame : true,
            store : subjectStore,
            cm : subjectCm,
            sm : subjectSm,
            tbar : [{
                text : "导入",
                iconCls : "import",
                handler : importIn
            }]
        });
        var subjectWindow = new Ext.Window({
            title : "问卷调查题目",
            layout : "fit",
            modal : true,
            // iconCls : "save",
            height : 400,
            width : 860,
            bodyStyle : 'padding:5px;',
            items : subjectGrid
        });
        subjectWindow.show();

        function importIn() {
            var rec = questionnaireSm.getSelected();
            if(rec==undefined){
                return false;
            }
            var questionnaireId = rec.get("id");
            var form = new Ext.form.FormPanel({
                fileUpload : true,
                frame : true,
                labelWidth: 30,
                url : "admin/questionnaire/subject/importIn",
                defaults : {
                    anchor : "100%",
                    allowBlank : false
                },
                items : [{
                    xtype : "hidden",
                    name : "questionnaireId",
                    value : questionnaireId
                }, {
                    fieldLabel : "文件",
                    emptyText: '请选择文件',
                    anchor: '100%',
                    xtype : "fileuploadfield",
                    name : 'file',
                    buttonText : "浏览"
                }],
                buttons : [{
                    text : "确定",
                    iconCls : "save",
                    handler : function(b){
                        submitForm(b.findParentByType("form"), refreshGrid);
                        function refreshGrid(){
                            subjectStore.load();
                        }
                    }
                }, {
                    text : "重置",
                    iconCls : "reset",
                    handler : function(b){
                        b.findParentByType("form").getForm().reset();
                    }
                }]
            });
            var win = new Ext.Window({
                title : "上传文件",
                width : 300,
                height : 150,
                layout : "fit",
                frame : true,
                items : [form]
            });
            win.show();
        }
    }
}

registerModule("admin.questionnaire.manage", admin.questionnaire.manage);